package com.icss.service.impl;

import com.icss.dao.IRecordDao;
import com.icss.domian.PageBean;
import com.icss.domian.QueryVO;
import com.icss.domian.VehicleIllegalRecord;
import com.icss.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.sql.SQLException;
import java.util.List;

@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    private IRecordDao recordDao;

    @Override
    public List<VehicleIllegalRecord> findAll(PageRequest pageable) {
        return recordDao.findAll();
    }

    @Override
    public List<QueryVO> findQueryVO() {
        return recordDao.findQueryVO();
    }

    @Override
    public Page<QueryVO> findPage(Pageable pageable) {
        return recordDao.findPage(pageable);
    }

    @Override
    public PageBean selectPageBean(int currentPage, int count) throws SQLException {
        PageBean pageBean = new PageBean();
        //封装currentPage
        pageBean.setCurrentPage(currentPage);
        //封装count，每页显示多少条
        pageBean.setCount(count);
        //封装总的条数
        int totalCount = recordDao.countAllPage();
        pageBean.setTotalCount(totalCount);
        //封装总页数
        pageBean.setTotalPage((int) Math.ceil((double)totalCount/count));
        pageBean.setQueryVOList(recordDao.findPage1((currentPage-1)*count, count));
        return pageBean;
    }


    @Override
    public Integer countAllPage() {
        return recordDao.countAllPage();
    }

    @Override
    public List<QueryVO> findByTimeAndCordLicense(String beginTime, String endTime, String RecordLicense) {
        return recordDao.findByTimeAndCordLicense(beginTime,endTime,RecordLicense);
    }

    @Override
    @Transactional
    public void updateIllegalItem(Integer userId, Integer illegalItemId, String illegalRecordTime, String illegalRecordLicense, String illegalRecordAddress) {
        recordDao.updateIllegalItem(userId,illegalItemId,illegalRecordTime,illegalRecordLicense,illegalRecordAddress);
    }

    @Override
    public void updateRecordLicenseByUserId(Integer userId, String illegalRecordLicense) {
        recordDao.updateRecordLicenseByUserId(userId,illegalRecordLicense);
    }

    @Override
    public void updateIllegalRecordStatusById(Integer illegalRecordId) {
        recordDao.updateIllegalRecordStatusById(illegalRecordId);
    }


}
